module Utils where

import Data.List (isPrefixOf)

splitAll :: Eq a => [a] -> [a] -> [[a]]
splitAll _ [] = []
splitAll delims xs = 
    token : splitAll delims rest
    where
        isDelimiter = (`elem` delims)
        token       = takeWhile (not . isDelimiter) xs
        rest        = dropWhile isDelimiter $ dropWhile (not . isDelimiter) xs

findSubList :: Eq a => [a] -> [a] -> Maybe Int
findSubList sub lst = doFindSubList 0 sub lst
    where
        doFindSubList :: Eq a => Int -> [a] -> [a] -> Maybe Int
        doFindSubList idx sub lst@(_:xs) 
            | length sub > length lst = Nothing
            | otherwise = if sub `isPrefixOf` lst 
                            then Just idx
                            else doFindSubList (idx+1) sub xs

split :: Eq a => [a] -> [a] -> [[a]]
split _ [] = []
split delims input = 
    case delimsIdx of
        Just idx -> token : (split delims rest)
            where
                token = take idx input
                rest = drop (idx + length delims) input
        Nothing -> [input]
    where
        delimsIdx = findSubList delims input
